<?php

/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
 */

class bm_categories_cya {

    var $code = 'bm_categories_cya';
    var $group = 'boxes';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;
    var $manufactures;
    var $manu_id;
    var $menu;
    var $flag_div = false;
    var $flag_one = false;
    var $jquery_css = '';

    function bm_categories_cya() {
        $this->title = 'Categories (cya)';
        $this->description = MODULE_BOXES_CATEGORIES_DESCRIPTION;

        if (defined('MODULE_BOXES_CATEGORIES_CYA')) {
            $this->sort_order = MODULE_BOXES_CATEGORIES_SORT_ORDER_CYA;
            $this->enabled = (MODULE_BOXES_CATEGORIES_CYA == 'True');

            $this->group = ((MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT_CYA == 'Left Column') ? 'boxes_column_left' : 'boxes_column_right');
        }
    }

    /**
     * function check sun cat
     * @author Nguyen Manh Hung
     * @param int $id
     * @return int
     */
    function check_sub_cat($id) {

        $check_query = tep_db_query("select count(c.categories_id) as count_row from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int) $id . "'");
        $value = tep_db_fetch_array($check_query);
        if ($value['count_row']) {
            return 1;
        } else {
            return 0;
        }
    }

    function get_menu() {
        global $cPath_array;
        $id = $cPath_array[count($cPath_array) - 1];
        //$subcategories_array = array();
        //$subcats = tep_get_subcategories($subcategories_array, $id);
        //$sql = 'SELECT DISTINCT p.manufacturers_id from products as p INNER JOIN products_description as pd on p.products_id = pd.products_id order by pd.products_viewed DESC';
        $sql = "select distinct m.manufacturers_id";
        $sql .= " from " . TABLE_PRODUCTS . " p left join " . TABLE_MANUFACTURERS . " m using(manufacturers_id)";
        $sql .= " , " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_CATEGORIES . " c, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c";
        $sql .= " where p.products_status = '1' and p.products_id = pd.products_id and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.categories_id=" . $id;
        $exit_m = array();
        $m_id_query = mysql_query($sql);
        while ($manu_id = tep_db_fetch_array($m_id_query)) {
            $exit_m[] = $manu_id['manufacturers_id'];
            $m_query = mysql_query("select manufacturers_name,manufacturers_id from " . TABLE_MANUFACTURERS . " where manufacturers_id = " . $manu_id['manufacturers_id'] . " order by manufacturers_id ASC");
            while ($manufacture = tep_db_fetch_array($m_query)) {
                $this->manufactures[] = $manufacture;
            }
        }

        /* $exit_id = implode(',', $exit_m);
          if (count($exit_m)) {
          $m_exit_query = mysql_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS . " where manufacturers_id not in ($exit_id)");
          } else {
          $m_exit_query = mysql_query("select manufacturers_id, manufacturers_name from " . TABLE_MANUFACTURERS);
          }

          while ($m_exit_qu = tep_db_fetch_array($m_exit_query)) {
          $this->manufactures[] = $m_exit_qu;
          } */
        return $this->manufactures;
    }

    function cya_count_products_in_price($category_id, $price_from, $price_to) {
        global $currencies, $currency;
        $products_count = 0;

        $currencies_array = $currencies->currencies;
        $cCurrentcy = $currencies_array[$currency];
        $price_to = $price_to / $cCurrentcy['value'];
        $price_from = $price_from / $cCurrentcy['value'];

        $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p.products_status = '1' and p2c.categories_id = '" . (int) $category_id . "' and p.products_price >= '" . $price_from . "' and p.products_price <= '" . $price_to . "'");

        $products = tep_db_fetch_array($products_query);
        $products_count += $products['total'];
        if ($products_count) {
            return '(' . $products_count . ')';
        }
        return '';
    }

    function cya_count_products_in_manu($category_id, $manu_id) {
        $products_count = 0;

        $products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p.products_status = '1' and p2c.categories_id = '" . (int) $category_id . "' and p.manufacturers_id ='" . $manu_id . "'");

        $products = tep_db_fetch_array($products_query);
        $products_count += $products['total'];

        if ($products_count) {
            return '(' . $products_count . ')';
        }
        return '';
    }

    function show_currency($value_input) {
        global $currency, $currencies;
        $currencies_array = $currencies->currencies;
        $cCurrentcy = $currencies_array[$currency];
        $rate_filter = $cCurrentcy['rate_filter'];
        $value_out = $value_input * $rate_filter;
        $value_out = number_format($value_out, 0);
        return $value_out;
    }

    function tep_show_category($counter) {
        global $tree, $categories_string, $cPath_array, $currency, $currencies;
        $currencies_array = $currencies->currencies;
        $cCurrentcy = $currencies_array[$currency];
        $this->flag_one = false;
        $cPath_array_new = $cPath_array;
        $id = $cPath_array[count($cPath_array) - 1];
        $parent = 0;
        if (count($cPath_array) - 2 >= 0) {
            $parent = $cPath_array[count($cPath_array) - 2];
        }
        $path_array = explode('_', $tree[$counter]['path']);
        $cid = $path_array[count($path_array) - 1];
        $cparent = $tree[$counter]['parent'];
        if (!$tree[$counter]['level']) {
            if ((is_array($cPath_array_new) ) && (in_array($cid, $cPath_array_new))) {
                $categories_string .='<div id="appMenu" class="d_get_content_top_cat">';
            } else {
                $categories_string .='<div id="appMenu">';
            }
        }

        //tu day
        if (!$tree[$counter]['level']) {
            if ((is_array($cPath_array_new) ) && (in_array($cid, $cPath_array) && $tree[$counter]['level'] == 0)) {
                $categories_string .= '<h3 class="ui-accordion-header ui-helper-reset ui-state-active ui-corner-top">';
                $categories_string .= '<div style="position: relative;">';
                $categories_string .= '<span class="ui-icon ui-icon-minus" style="float: left;"></span>';
            } else {
                $categories_string .= '<h3 class="ui-accordion-header ui-helper-reset ui-state-default ui-corner-all">';
                $categories_string .= '<div>';
                $categories_string .= '<span class="ui-icon ui-icon-plus" style="float: left;"></span>';
            }
        }
        if (count($cPath_array) >= 1) {
            array_pop($cPath_array_new);
            if (!in_array($cparent, $cPath_array_new) || $id == $cid || in_array($cid, $cPath_array_new)) {
                //cho hien ra binh thuong
                $categories_string .= '<a href="';

                if ($tree[$counter]['parent'] == 0) {
                    $cPath_new = 'cPath=' . $counter;
                } else {
                    $cPath_new = 'cPath=' . $tree[$counter]['path'];
                }
                $url = tep_href_link(FILENAME_DEFAULT, $cPath_new);
                $categories_string .= tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">';
                $categories_string .= '<div class="url"><input type="hidden" value="' . $url . '"/></div>';
                if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                    $categories_string .= '<strong>';
                }

                // display category name
                $padding_left = 0;
                $width_right = 120;
                $width_sub = 132;
                for ($i = 0; $i < $tree[$counter]['level']; $i++) {
                    //$categories_string .= "&nbsp;&nbsp;";
                    $padding_left+=5;
                }
                $width_right = $width_right - $padding_left;
                $width_sub = $width_sub - $padding_left;
                if (!$tree[$counter]['level'] && !in_array($counter, $cPath_array)) {
                    $categories_string .= '<span class="block_title" style="margin-left: ' . $padding_left . 'px; width:' . $width_right . 'px;">' . $tree[$counter]['name'];
                } else {
                    if (!$tree[$counter]['level']) {
                        $categories_string .= '<span class="block_title border_bottom" style="margin-left: ' . $padding_left . 'px; width:' . $width_right . 'px;">' . $tree[$counter]['name'];
                    } else {
                        $categories_string .= '<span class="block_title border_bottom" style="margin-left: ' . $padding_left . 'px; width:' . $width_sub . 'px;">' . $tree[$counter]['name'];
                    }
                }

                if (tep_has_category_subcategories($counter)) {
                    //$categories_string .= '-&gt;';
                }
                if (SHOW_COUNTS == 'true') {
                    $products_in_category = tep_count_products_in_category($counter);
                    if ($products_in_category > 0 && $tree[$counter]['level']) {
                        $categories_string .= '&nbsp;(' . $products_in_category . ')';
                    }
                }
                $categories_string .= '</span><div class="clear"></div>';
                if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                    $categories_string .= '</strong>';
                }

                $categories_string .= '</a>';

                if ($counter == $cPath_array[0]) {
                    $categories_string .= '<span class="icon_back"><a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_image(DIR_WS_IMAGES . 'close-md-new.png', 'back') . '</a></span>';
                }

                //$categories_string .= '<div class="div_background"></div>';
                if ($id == $cid && !$this->check_sub_cat($id)) {
                    if (!tep_has_category_subcategories($counter) && !$tree[$counter]['level']) {
                        $this->flag_one = true;
                        $categories_string.= '</h3>';

                        $categories_string .= '<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="padding-left: 5px;">';
                    }
                    $this->flag_div = true;
                    if (!count($this->manufactures)) {
                        $menu = $this->get_menu();
                    }

                    if ($this->flag_one) {
                        $categories_string.=
                                '<div class="" style="margin: 0px 5px 5px 3px; width:135px; padding-top: 5px;"><span style="color:#E17009; font-weight: bold;">Price</span>' .
                                '<div id="any_price" class="anyPrice" style="margin:5px 0px 0px 10px; "><a href="#"> &laquo; Any price</a></div>' .
                                '<div class="price" style="margin-left:10px;">';
                    } else {
                        $categories_string.=
                                '<div class="" style="margin: 15px 5px 5px 3px; width:135px;"><span style="color:#E17009; font-weight: bold;">Price</span>' .
                                '<div id="any_price" class="anyPrice" style="margin:5px 0px 0px 10px; "><a href="#"> &laquo; Any price</a></div>' .
                                '<div class="price" style="margin-left:10px; margin-top: 5px;">';
                    }

                    if (SHOW_COUNTS == 'true') {
                        $categories_string .= '<div> <a href="javascript:void(0);" onclick="choose_price(0,' . $this->show_currency(40) . ');">Under ' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, 0, $this->show_currency(40)) . '</div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(40) . ',' . $this->show_currency(100) . ');">' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, $this->show_currency(40), $this->show_currency(100)) . '</div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(100) . ',' . $this->show_currency(200) . ');">' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, $this->show_currency(100), $this->show_currency(200)) . '</div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(200) . ',' . $this->show_currency(400) . ');">' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, $this->show_currency(200), $this->show_currency(400)) . '</div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(400) . ',0);">Over ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a>&nbsp;' . $this->cya_count_products_in_price($id, $this->show_currency(400), 9999999999) . '</div>';
                    } else {
                        $categories_string .= '<div> <a href="javascript:void(0);" onclick="choose_price(0,' . $this->show_currency(40) . ');">Under ' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(40) . ',' . $this->show_currency(100) . ');">' . $cCurrentcy['symbol_left'] . $this->show_currency(40) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(100) . ',' . $this->show_currency(200) . ');">' . $cCurrentcy['symbol_left'] . $this->show_currency(100) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(200) . ',' . $this->show_currency(400) . ');>' . $cCurrentcy['symbol_left'] . $this->show_currency(200) . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a></div>' .
                                '<div> <a href="javascript:void(0);" onclick="choose_price(' . $this->show_currency(400) . ',0);">Over ' . $cCurrentcy['symbol_left'] . $this->show_currency(400) . $cCurrentcy['symbol_right'] . '</a></div>';
                    }

                    $categories_string .= '</div>' .
                            '<div style="margin-top: 5px;margin-left: 5px;">' . $cCurrentcy['symbol_left'] . '<input class="input1" id="ip_price_from" value="" type="text" size="2"/>' . $cCurrentcy['symbol_right'] . ' to ' . $cCurrentcy['symbol_left'] . '<input class="input2" id="ip_price_to" value="" type="text" size="2">' . $cCurrentcy['symbol_right'] . '
                                <span  style="margin-right:0px; position: relative;"><a href="javascript:void(0)" onclick="search_price()" ><img src="images/go_icon.gif" width="13px" height="13px" border="0" style="position: relative; top: 4px;" /></a></span>
                            </div>' .
                            '<input type="hidden" name="cya" id="cya" value="" />' .
                            '<input type="hidden" name="price_to" id="price_to" value="" />' .
                            '<input type="hidden" name="price_from" id="price_from" value="" />' .
                            '<input type="hidden" name="filter_sort" id="filter_sort" value="2a"/>' .
                            '<input type="hidden" name="filter_page" id="filter_page" value="1" />' .
                            '</div>';

                    $categories_string.= '<div class="more" style="margin: 10px 5px 5px 5px; width: 135px;"><span style="color:#E17009; font-weight: bold;">Brand</span>';
                    $categories_string.= '<div id="clear" style="float:left; margin: 0px 10px 0px 10px; width: 135px;"><a href="#">Clear</a></div>';
                    $categories_string.= '<div style="margin-left:10px; margin-top:5px; color: #2E6E9E;" id="show_manu">';
                    $i = 0;
                    if (is_array($menu) && count($menu)) {
                        foreach ($menu as $value) {
                            $categories_string.= '<div id="mamu_' . $value['manufacturers_id'] . '"> <input name="manu_ids[]" id="manu_ids[]" type="checkbox" value="' . $value['manufacturers_id'] . '"/>' . $value['manufacturers_name'] . '&nbsp;' . $this->cya_count_products_in_manu($id, $value['manufacturers_id']) . '</div>';
                            $i++;
                        }
                    }


                    $categories_string.= '</div>';
                    $categories_string.= '<div id="seemore" class="see_more"  style="margin-bottom:2px; margin-left: 20px;"><a href="#"> &raquo; See more</a></div>';
                    $categories_string.='<br />';
                    if ($this->flag_one) {
                        $categories_string.='</div>';
                    }
                    $categories_string.='</div>';
                    //$categories_string .= $str_css;
                    //$this->jquery_css .= $str_jquery.$str_css;
                }
            }
        } else {
            //cho hien ra binh thuong
            for ($i = 0; $i < $tree[$counter]['level']; $i++) {
                $categories_string .= "&nbsp;&nbsp;";
            }
            $categories_string .= '<a href="';

            if ($tree[$counter]['parent'] == 0) {
                $cPath_new = 'cPath=' . $counter;
            } else {
                $cPath_new = 'cPath=' . $tree[$counter]['path'];
            }

            $categories_string .= tep_href_link(FILENAME_DEFAULT, $cPath_new) . '">';

            if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                $categories_string .= '<strong>';
            }

            // display category name
            $categories_string .= $tree[$counter]['name'];

            if (isset($cPath_array) && in_array($counter, $cPath_array)) {
                $categories_string .= '</strong>';
            }

            if (tep_has_category_subcategories($counter)) {
                //$categories_string .= '-&gt;';
            }

            $categories_string .= '</a>';

            if (SHOW_COUNTS == 'true') {
                $products_in_category = tep_count_products_in_category($counter);
                if ($products_in_category > 0 && $tree[$counter]['level']) {
                    $categories_string .= '&nbsp;(' . $products_in_category . ')';
                }
            }
        }
        if (!$tree[$counter]['level']) {
            $categories_string .='</h3>';
            if ($tree[$counter]['next_id'] != false) {
                if (!$tree[$tree[$counter]['next_id']]['level']) {
                    $categories_string .='<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="display:none; padding-left: 5px;">';
                } else {
                    $categories_string .='<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="padding-left: 5px;">';
                }
            } else {
                $categories_string .='<div class="ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="display:none; padding-left: 5px;">';
            }
        }

        //den day

        if ($tree[$counter]['next_id'] != false) {
            if (!$tree[$tree[$counter]['next_id']]['level']) {
                $categories_string .='</div></div>';
            }
            $this->tep_show_category($tree[$counter]['next_id']);
        } else {
            if ($this->flag_div = true && !$this->flag_one) {
                $categories_string .='</div>';
            } else {
                $categories_string .='</div></div>';
            }
        }
        /*$u_agent = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/MSIE/i', $u_agent)) {
            $categories_string .='</div></div></div>';
        }*/
    }

    function getData() {
        global $categories_string, $tree, $languages_id, $cPath, $cPath_array, $currency, $currencies;
        //print_r($currency);
        //print_r($currencies);
        $categories_string = '';
        $categories_string_top = '';
        $tree = array();

        $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '0' and c.categories_id = cd.categories_id and cd.language_id='" . (int) $languages_id . "' order by sort_order, cd.categories_name");
        while ($categories = tep_db_fetch_array($categories_query)) {
            $tree[$categories['categories_id']] = array('name' => $categories['categories_name'],
                'parent' => $categories['parent_id'],
                'level' => 0,
                'path' => $categories['categories_id'],
                'next_id' => false);

            if (isset($parent_id)) {
                $tree[$parent_id]['next_id'] = $categories['categories_id'];
            }

            $parent_id = $categories['categories_id'];

            if (!isset($first_element)) {
                $first_element = $categories['categories_id'];
            }
        }

        if (tep_not_null($cPath)) {
            $new_path = '';
            reset($cPath_array);
            while (list($key, $value) = each($cPath_array)) {
                unset($parent_id);
                unset($first_id);
                $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int) $value . "' and c.categories_id = cd.categories_id and cd.language_id='" . (int) $languages_id . "' order by sort_order, cd.categories_name");
                if (tep_db_num_rows($categories_query)) {
                    $new_path .= $value;
                    while ($row = tep_db_fetch_array($categories_query)) {
                        $tree[$row['categories_id']] = array('name' => $row['categories_name'],
                            'parent' => $row['parent_id'],
                            'level' => $key + 1,
                            'path' => $new_path . '_' . $row['categories_id'],
                            'next_id' => false);

                        if (isset($parent_id)) {
                            $tree[$parent_id]['next_id'] = $row['categories_id'];
                        }

                        $parent_id = $row['categories_id'];

                        if (!isset($first_id)) {
                            $first_id = $row['categories_id'];
                        }

                        $last_id = $row['categories_id'];
                    }
                    $tree[$last_id]['next_id'] = $tree[$value]['next_id'];
                    $tree[$value]['next_id'] = $first_id;
                    $new_path .= '_';
                } else {
                    break;
                }
            }
        }

        //echo '<pre>';
        //print_r($tree);
        //echo '</pre>';
        //print_r($first_element);die();
        $this->tep_show_category($first_element);
        $str_jquery = '<script type="text/javascript">
                        
    
                        $(document).ready(function()
                        { 
                            $("div#appMenu").hover(function(e) {
                                $(this).find("h3").addClass("ui-state-hover");    
                            }, function() {
                                $(this).find("h3").removeClass("ui-state-hover");   
                            });
                            var content_cat = $("div.d_get_content_top_cat").html();                            
                            $("div#d_put_content_top_cat").html(content_cat);                            
                            $("div.d_get_content_top_cat").hide();
                            $("div.d_get_content_top_cat").html("");
                            var i=0;
                            var content = $(".more input").length;
                            $(".more div").each(function(index){
                                if(index>21){
                                    $(this).addClass("deactive");
                                    $(".see_more").removeClass("see_more");
                                    $("div#seemore").removeClass("deactive");
                                }
                            });

                            $("#seemore").click(function(){
                                $(".more div").removeClass("deactive");
                                $(this).addClass("see_more");
                                return false;
                            });
                            
                            $("div#show_manu input:checkbox").click(function(){
                                $("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);                                
                                filter_product();
                            });

                            $("#clear").click(function(){
                                $.each($("div.font_bold"), function() { 
                                    $(this).removeClass("font_bold");
                                });
                                $("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);
                                $(this).hide();
                                $("input:checkbox").removeAttr("checked");
                                $(".contentText").empty();
                                filter_product();
                                return false;

                            });
                            
                            $("#any_price").click(function(){
                                $("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);
                                $(".price div").removeClass("a12");
                                $(".price div").show();
                                $(this).hide();
                                $(".input1").attr("value", "");
                                $(".input2").attr("value", "");
                                $("input#price_from").attr("value", "");
                                $("input#price_to").attr("value", "");
                                filter_product();
                                return false;
                            });
                            
                        });
                        
                        // function choose price
                        function choose_price(priceF, priceT){
                            $("input#filter_sort").val("2a");
                            $("input#filter_page").val(1);
                            $(".price div").not(".a12").hide();
                            $(".anyPrice").show();
                            if(priceF>0){
                                $(".input1").attr("value", priceF);
                                $("input#price_from").attr("value", priceF);
                            }else{
                                $(".input1").attr("value", 0);
                                $("input#price_from").attr("value", 0);
                            }
                            if(priceT>0){
                                $(".input2").attr("value", priceT);
                                $("input#price_to").attr("value", priceT);
                            }else{
                                $(".input2").attr("value", "");
                                $("input#price_to").attr("value", "");
                            }
                            filter_product();
                            return false;
                        }
                        // get all manufacturies_id
                        function get_checked_manu(){
                             var allVals = ""; 
                             $.each($("div.font_bold"), function() {
                                $(this).removeClass("font_bold");
                             });
                             //outputs: 1:Red 2:Orange 3:Green
                             $("input[name=\'manu_ids[]\']:checkbox:checked").each(function() {
                                $("#clear").show();
                                $("div#mamu_"+$(this).val()).addClass("font_bold");
                                allVals = allVals+"_"+$(this).val();                             
                             });
                             if(allVals!=""){
                                allVals = allVals.substring(1);
                             }
                             return allVals;
                        }
                        
                        // action click go
                        function search_price(){
                            var price_from = $("input#ip_price_from").val();
                            var price_to = $("input#ip_price_to").val();
                            if(price_from|| price_to){
                                $("input#filter_sort").val("2a");
                                $("input#filter_page").val(1);
                                $("input#price_from").attr("value", price_from);
                                $("input#price_to").attr("value", price_to); 
                                $(".price div").not(".a12").hide();
                                $(".anyPrice").show();
                                filter_product();
                            }                            
                        }
                        
                        // post and filter product
                        function filter_product(){
                            $(".contentText").html("<img src=\'images/loading29.gif\' border=\'0\'>");
                            $("form[name=\'filter\']").addClass("deactive");
                            var cid = $("input#cid").val();
                            var cPath = $("input#cPath").val();
                            var price_from = $("input#price_from").val();
                            var price_to = $("input#price_to").val();
                            var manu_ids = get_checked_manu();
                            var sort = $("input#filter_sort").val();
                            var page = $("input#filter_page").val();                           
                            $.post("filter.php?categories_id="+cid+"&price_from="+price_from+"&price_to="+price_to+"&manufacturers_id="+manu_ids+"&sort="+sort+"&page="+page+"&cPath="+cPath,
                                function(data) {
                                    $(".contentText").html(data);
                            });
                            //$("html, body").animate({ scrollTop: 0 }, "slow");
                        }
                        
                        function change_sort(sort_by){
                            var cur_sort = $("input#filter_sort").val();
                            var fsort = parseInt(cur_sort.substring(0, 1));
                            var lsort = cur_sort.substring(1, 2);
                            
                            if(sort_by==fsort){
                                if(lsort=="a"){
                                    $("input#filter_sort").val(sort_by+"d");
                                }else{
                                    $("input#filter_sort").val(sort_by+"a");
                                }
                            }else{
                                $("input#filter_sort").val(sort_by+"a");
                            }
                            $("input#filter_page").val(1);
                            filter_product();
                        }
                        
                        function change_page(page_new){
                            $("input#filter_page").val(page_new);
                            filter_product();
                        }
                        


                        var compare_text = "' . IMAGE_BUTTON_COMPARE . '";
                        var products_text = "' . IMAGE_BUTTON_COMPARE_PRODUCTS . '";
                        function validateButton() {	
                                var getVal = $("input:checkbox:checked").length;
                            var compare = [];
                            var cookie_compare = $.cookie(\'compare\');
                            if (cookie_compare) {
                                compare = cookie_compare.split(\',\');
                            }
                                getVal = compare.length;

                                        if (getVal <  2)
                                        {
                                                alert("' . TEXT_AT_LEAST_N_PRODUCTS . '");
                                                return false;		      
                                        }
                                        if (getVal >  4)
                                        {
                                        alert("' . TEXT_COMPARE_MAX_N_PRODUCTS . '");
                                        return false;		
                                }
                        }
                        if (!Array.prototype.indexOf)
                        {
                          Array.prototype.indexOf = function(elt /*, from*/)
                          {
                            var len = this.length;

                            var from = Number(arguments[1]) || 0;
                            from = (from < 0)
                                 ? Math.ceil(from)
                                 : Math.floor(from);
                            if (from < 0)
                              from += len;

                            for (; from < len; from++)
                            {
                              if (from in this &&
                                  this[from] === elt)
                                return from;
                            }
                            return -1;
                          };
                        }

                        function updateCompare() {
                                var compare = [];
                                var cookie_compare = $.cookie(\'compare\');
                                if (cookie_compare) {
                                        compare = cookie_compare.split(\',\');
                                }
                                $(\'input:checkbox\').each(function () {
                                        if (this.checked) {
                                                if (compare.indexOf(this.value) == -1) {
                                                        compare.push(this.value);
                                                }
                                        } else {
                                                if (compare.indexOf(this.value) > -1) {
                                                        compare[compare.indexOf(this.value)] = null;
                                                }
                                        }
                                });
                                cookie_compare = compare.join(\',\');
                                cookie_compare = cookie_compare.replace(/^([,]+)/, \'\'); 
                                cookie_compare = cookie_compare.replace(/([,]{2,})/, \',\'); 
                                cookie_compare = cookie_compare.replace(/([,]+)$/, \'\'); 

                                $.cookie(\'compare\', cookie_compare);
                                updateCompareButton();
                        }
                        function updateCompareButton() {
                            var compare = [];
                                var cookie_compare = $.cookie(\'compare\');
                                if (cookie_compare) {
                                        compare = cookie_compare.split(\',\');
                                }
                                if (compare.length > 0) {
                                        $("#compare_button_clear").find(":button").button({disabled: false});
                                } else {
                                        $("#compare_button_clear").find(":button").button({disabled: true});
                                }
                            $("#compare_button").find(":button").button(\'option\', \'label\', compare_text + \' \' + compare.length + \' \' + products_text);
                        }
                        function clearCompareCheckboxes() {
                                $.cookie(\'compare\', \'\');
                                setCompareCheckboxes();
                        }
                        function setCompareCheckboxes() {
                            var compare = [];
                            var cookie_compare = $.cookie(\'compare\');
                            if (cookie_compare) {
                                compare = cookie_compare.split(\',\');
                            }

                            $(\'input:checkbox\').each(function () {
                                if (compare.indexOf(this.value) != -1) {
                                    this.checked = true;
                                } else {
                                    this.checked = false;
                                        };
                            });
                                updateCompareButton();
                        }
                        $().ready(function () {
                                setCompareCheckboxes();
                                $("#compare_button_clear").click(function() {clearCompareCheckboxes();return false;});
                        });

                        </script>';

        $str_css = '<style type="text/css"/>
                         .deactive{
                            display: none;
                         }
                         .see_more{
                            display: none;
                         }                         
                         .anyPrice{
                            display: none;
                         }
                         #clear{
                                 display: none;
                             }

                     </style>';
        $this->jquery_css .= $str_jquery . $str_css;
        $data = $this->jquery_css;
        $data .= '<div id="d_put_content_top_cat" style="margin-bottom: 5px;"></div><div class="clear"></div>';
        $data .= '<div id="aappMenu">' . $categories_string . '</div>';
        if ($this->flag_div) {
            $data.= '</div>';
        }
        $data.= ' <input type="hidden" name="cid" id="cid" value="' . $cPath_array[count($cPath_array) - 1] . '"/>';
        $data.= ' <input type="hidden" name="cPath" id="cPath" value="' . $cPath . '"/>';


        return $data;
    }

    function execute() {
        global $SID, $oscTemplate;

        if ((USE_CACHE == 'true') && empty($SID)) {
            $output = tep_cache_categories_box();
        } else {
            $output = $this->getData();
        }

        $oscTemplate->addBlock($output, $this->group);
    }

    function isEnabled() {
        return $this->enabled;
    }

    function check() {
        return defined('MODULE_BOXES_CATEGORIES_CYA');
    }

    function install() {
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Search Module', 'MODULE_BOXES_CATEGORIES_CYA', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT_CYA', 'Left Column', 'Should the module be loaded in the left or right column?', '6', '1', 'tep_cfg_select_option(array(\'Left Column\', \'Right Column\'), ', now())");
        tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_CATEGORIES_SORT_ORDER_CYA', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
    }

    function remove() {
        tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
        return array('MODULE_BOXES_CATEGORIES_CYA', 'MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT_CYA', 'MODULE_BOXES_CATEGORIES_SORT_ORDER_CYA');
    }

}

?>
